package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkSpaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

@Service
public class WorkSpaceServiceImpl implements WorkSpaceService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private UserMapper userMapper;
    /**
     * 工作台中的今日数据统计
     * @return
     */
    @Override
    public BusinessDataVO businessData(LocalDateTime startTime,LocalDateTime endTime) {
       /* // 2023-12-04 00:00:00
        LocalDateTime startTime = LocalDateTime.now().with(LocalTime.MIN);
        //// 2023-12-04 23:59:59
        LocalDateTime endTime = LocalDateTime.now().with(LocalTime.MAX);*/

        /*
        Double turnover;//营业额  --> 当日已完成订单的总金额
        Integer validOrderCount;//有效订单数 --> 当日已完成订单的数量
        Double orderCompletionRate;//订单完成率 --> 有效订单数 / 订单总数
        Double unitPrice;//平均客单价  --> 营业额 / 有效订单数
        Integer newUsers;//新增用户数  -->  当日新增用户的数量
         */

        HashMap<Object, Object> map = new HashMap<>();
        map.put("begin",startTime);
        map.put("end",endTime);

        // 查询总订单数
        Integer totalCount = orderMapper.countByMap(map);

        map.put("status", Orders.COMPLETED);
        //查询营业额
        Double turnover = orderMapper.sumByMap(map);
        turnover = turnover == null ? 0.0 : turnover;

        //查询有效订单数
        Integer validOrderCount = orderMapper.countByMap(map);

        //计算平均客单价  营业额 / 有效订单数
        Double unitPrice = 0.0;
        if(validOrderCount != 0){
            unitPrice = turnover / validOrderCount;
        }

        //计算订单完成率 有效订单数 / 订单总数
        Double orderCompletionRate = 0.0;
        if(totalCount != 0 ){
            orderCompletionRate = validOrderCount.doubleValue() / totalCount;
        }

        //查询新增用户数
        Integer newUsers = userMapper.countByMap(map);


        BusinessDataVO businessDataVO = new BusinessDataVO();
        businessDataVO.setUnitPrice(unitPrice);
        businessDataVO.setNewUsers(newUsers);
        businessDataVO.setTurnover(turnover);
        businessDataVO.setValidOrderCount(validOrderCount);
        businessDataVO.setOrderCompletionRate(orderCompletionRate);
        return businessDataVO;
    }

    /**
     * 查询订单管理数据
     * @return
     */
    @Override
    public OrderOverViewVO overviewOrders() {
        HashMap<Object,Object> map = new HashMap();
        map.put("begin", LocalDateTime.now().with(LocalTime.MIN));
        map.put("end", LocalDateTime.now().with(LocalTime.MAX));
        map.put("status", Orders.TO_BE_CONFIRMED);

        //待接单
        Integer waitingOrders = orderMapper.countByMap(map);

        //待派送
        map.put("status", Orders.CONFIRMED);
        Integer deliveredOrders = orderMapper.countByMap(map);

        //已完成
        map.put("status", Orders.COMPLETED);
        Integer completedOrders = orderMapper.countByMap(map);

        //已取消
        map.put("status", Orders.CANCELLED);
        Integer cancelledOrders = orderMapper.countByMap(map);

        //全部订单
        map.put("status", null);
        Integer allOrders = orderMapper.countByMap(map);

        return OrderOverViewVO.builder()
                .waitingOrders(waitingOrders)
                .deliveredOrders(deliveredOrders)
                .completedOrders(completedOrders)
                .cancelledOrders(cancelledOrders)
                .allOrders(allOrders)
                .build();
    }

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 查询菜品总览
     *
     * @return
     */
    public DishOverViewVO getDishOverView() {
        Map map = new HashMap();
        map.put("status", StatusConstant.ENABLE);
        Integer sold = dishMapper.countByMap(map);

        map.put("status", StatusConstant.DISABLE);
        Integer discontinued = dishMapper.countByMap(map);

        return DishOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }

    /**
     * 查询套餐总览
     *
     * @return
     */
    public SetmealOverViewVO getSetmealOverView() {
        Map map = new HashMap();
        map.put("status", StatusConstant.ENABLE);
        Integer sold = setmealMapper.countByMap(map);

        map.put("status", StatusConstant.DISABLE);
        Integer discontinued = setmealMapper.countByMap(map);

        return SetmealOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }
}
